# comment the following line to build for x86
CROSS_PREFIX := arm-linux-

# Application name
APP_NAME := sDesktop
#APP_SUBNAME := B
APP_PATH := Application/$(APP_NAME)
USR_LOCAL := root/.usr/.local
VERSION := 1.0.0

# add your project folder(s) to the fallowing Variable
PROJECTS := \
#	html/cgi

QT_PROJECTS := \
	sDesktop

# Install folder name
INSTALL_DIR := rootfs

# Install List
# List for wsncomm support
PRODUCT_COPY_FILES += \
	simsun.ttc:$(USR_LOCAL)/Trolltech/QtEmbedded-4.7.0-arm/lib/fonts/simsun.ttc \
	depends/libQtWebKit.so.4.7.0:$(USR_LOCAL)/Trolltech/QtEmbedded-4.7.0-arm/lib/libQtWebKit.so.4.7.0

PRODUCT_COPY_EXECUTABLES += \
	load.app:$(APP_PATH)/load.app \
	sDesktop/sDesktop:$(APP_PATH)/sDesktop

#PRODUCT_EXECUTABLES += \
#	root/www/cgi-bin/*.cgi \

PRODUCT_COPY_FOLDERS += \
	sDesktop/qml:$(APP_PATH)/qml \
	depends/imports:$(USR_LOCAL)/Trolltech/QtEmbedded-4.7.0-arm/imports
	
# Install Link List
PRODUCT_LINK_FILES += \
	/$(APP_PATH)/load.app:etc/rc.d/rcA.d/0$(APP_NAME) \
	libQtWebKit.so.4.7.0:$(USR_LOCAL)/Trolltech/QtEmbedded-4.7.0-arm/lib/libQtWebKit.so.4.7 \
	libQtWebKit.so.4.7.0:$(USR_LOCAL)/Trolltech/QtEmbedded-4.7.0-arm/lib/libQtWebKit.so.4 \
	libQtWebKit.so.4.7.0:$(USR_LOCAL)/Trolltech/QtEmbedded-4.7.0-arm/lib/libQtWebKit.so

###   ###  #   #  ##  #####     # #   ###  ###  ##### ##### #   #
#  # #   # ##  # ##     #       ###  #   # #  #   #   #      # #
#  # #   # #  ##        #      # # # #   # #  #   #   ###     #
###   ###  #   #        #      #   #  ###  ###  ##### #       #

# WARNING: DON'T MODIFY THE FALLOWING LINES

all: qtmakefile
	@$(foreach p,$(PROJECTS),CROSS_PREFIX=$(CROSS_PREFIX) make -C $(p);)
	@$(foreach p,$(QT_PROJECTS),make -C $(p);)
	@echo "Build Complete!"

install: all addon_install_deps
	@echo "Prepare rootfs..."
	-@$(foreach p,$(PRODUCT_EXECUTABLES),chmod a+x $(INSTALL_DIR)/$(p))
	-@find $(INSTALL_DIR) -name ".svn" | xargs -r rm -rf
	-@find $(INSTALL_DIR) ! -name "*.ko" | xargs -r file | grep "not stripped" | cut -d: -f1 | xargs -r $(CROSS_PREFIX)strip

uninstall: addon_uninstall_deps

deb: install
	-@INSTALL_DIR=$(INSTALL_DIR) PRODUCT_NAME=$(APP_NAME) VERSION=$(VERSION) ./makedeb

rootfs: install
	@echo "Building full rootfs..."
	-@sudo PRODUCT_NAME=$(APP_NAME) PRODUCT_SUBNAME=$(APP_SUBNAME) ./makeyaffsroot rootfs
	@echo "Installation Done!"

image: install
	@echo "Building rootfs Images..."
	-@sudo PRODUCT_NAME=$(APP_NAME) PRODUCT_SUBNAME=$(APP_SUBNAME) ./makeyaffsroot
	@echo "Installation Done!"

clean: qtmakefile
	-@rm -rf rootfs
	@$(foreach p,$(PROJECTS),make -C $(p) clean;)
	-@$(foreach p,$(QT_PROJECTS),make -C $(p) distclean;)
	-@$(foreach p,$(QT_PROJECTS),rm -rf $(p)/.obj $(p)/.moc $(p)/.uic;)

QT_MAKEFILE := $(foreach d,$(QT_PROJECTS),$(d)/Makefile)

qtmakefile: $(QT_MAKEFILE)

%/Makefile:
	@echo "Generating Makefile for $(dir $@)..."
	@cd $(dir $@) && qmake

empty :=
space := $(empty) $(empty)

define word-colon
$(word $(1),$(subst :,$(space),$(2)))
endef

define append-path
$(subst //,/,$(1)/$(2))
endef

define copy-one-file
$(2): $(1) FORCE
	@echo "Copy File: $$< -> $$@"
	@mkdir -p $$(dir $$@)
	@cp -a $$< $$@
endef

define rm-one-file
rm_$(2): $(2) FORCE
	@echo "Remove File: $$<"
	@rm -rf $$<
endef

define copy-one-executable
$(2): $(1) FORCE
	@echo "Copy Executable: $$< -> $$@"
	@mkdir -p $$(dir $$@)
	@cp -a $$< $$@
	@chmod a+x $$@
endef

define copy-one-folder
$(2): $(1) FORCE
	@echo "Copy Folder: $$< -> $$@"
	@mkdir -p $$@
	@cp -a $$</* $$@
endef

define rm-one-folder
rm_$(2): $(2) FORCE
	@echo "Remove Folder: $$<"
	@rm -rf $$<
endef

define link-one-file
$(1):

$(2): $(1) FORCE
	@echo "Link: $$@ -> $$<"
	@mkdir -p $$(dir $$@)
	@rm -f $$@
	@ln -s $$< $$@
endef

FORCE:

$(foreach cf,$(PRODUCT_COPY_FILES), \
  $(eval _src := $(call word-colon,1,$(cf))) \
  $(eval _dest := $(call append-path,$(INSTALL_DIR),$(call word-colon,2,$(cf)))) \
  $(eval $(call copy-one-file,$(_src),$(_dest))) \
  $(eval addon_copy_deps += $(_dest)) \
  $(eval $(call rm-one-file,$(_src),$(_dest))) \
  $(eval addon_rm_deps += rm_$(_dest)) \
 )

$(foreach cf,$(PRODUCT_COPY_EXECUTABLES), \
  $(eval _src := $(call word-colon,1,$(cf))) \
  $(eval _dest := $(call append-path,$(INSTALL_DIR),$(call word-colon,2,$(cf)))) \
  $(eval $(call copy-one-executable,$(_src),$(_dest))) \
  $(eval addon_copy_deps += $(_dest)) \
  $(eval $(call rm-one-file,$(_src),$(_dest))) \
  $(eval addon_rm_deps += rm_$(_dest)) \
 ) 

$(foreach cf,$(PRODUCT_COPY_FOLDERS), \
  $(eval _src := $(call word-colon,1,$(cf))) \
  $(eval _dest := $(call append-path,$(INSTALL_DIR),$(call word-colon,2,$(cf)))) \
  $(eval $(call copy-one-folder,$(_src),$(_dest))) \
  $(eval addon_copy_deps += $(_dest)) \
  $(eval $(call rm-one-folder,$(_src),$(_dest))) \
  $(eval addon_rm_deps += rm_$(_dest)) \
 )

$(foreach cf,$(PRODUCT_LINK_FILES), \
  $(eval _src := $(call word-colon,1,$(cf))) \
  $(eval _dest := $(call append-path,$(INSTALL_DIR),$(call word-colon,2,$(cf)))) \
  $(eval $(call link-one-file,$(_src),$(_dest))) \
  $(eval addon_link_deps += $(_dest)) \
  $(eval $(call rm-one-file,$(_src),$(_dest))) \
  $(eval addon_rmlink_deps += rm_$(_dest)) \
 )

addon_install_deps: $(addon_copy_deps) $(addon_link_deps)
addon_uninstall_deps: $(addon_rm_deps) $(addon_rmlink_deps)
